package com.example.nfcindoornav;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.tech.Ndef;
import android.nfc.tech.NdefFormatable;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	private NfcAdapter mNfcAdapter;
	private PendingIntent mPendingIntent;
	private IntentFilter[] mIntentFilters;
    private String[][] mTechLists;
	//String urlAddress ="";


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		//TextView Show NFC is aviable or not
		TextView myText = (TextView) findViewById(R.id.scuta_nfc_nev_check_adapter_text);
		

		// Write NFC Button Mode
		Button writeNfcMode = (Button) findViewById(R.id.scuta_nfc_nev_writeTag_btn);
		writeNfcMode.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				Intent myIntent = new Intent(view.getContext(),
						ActivityWriteNfc.class);
				startActivityForResult(myIntent, 0);

			}
		});

		// Read NFC Button Mode
		Button readNfcMode = (Button) findViewById(R.id.scuta_nfc_nev_readTag_btn);
		readNfcMode.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				Intent myIntent = new Intent(view.getContext(),
						ActivityReadNfc.class);
				startActivityForResult(myIntent, 0);

			}
		});
		
		//Map Mode Buton
		Button mapMode = (Button) findViewById(R.id.scuta_nfc_nev_map_btn);
		mapMode.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View view) {
				Intent myIntent = new Intent(view.getContext(),ActivityMap.class);
				startActivityForResult(myIntent, 0);
				
			}
		});

		mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
		mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
				getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
		IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
		mIntentFilters = new IntentFilter[] { ndef, };
		mTechLists = new String[][] { new String[] { Ndef.class.getName() },
				new String[] { NdefFormatable.class.getName() } };
		
		if (mNfcAdapter == null) {
			myText.setText("NFC is not available for the device !!!");
		} else {
			myText.setText("NFC is available for the device");
		}

	}
	
	@Override
	protected void onResume() {
		super.onResume();
		if(mNfcAdapter != null) {

			if (!mNfcAdapter.isEnabled()){
				
	            LayoutInflater inflater = getLayoutInflater();
	        	View dialoglayout = inflater.inflate(R.layout.nfc_setting_layout,(ViewGroup) findViewById(R.id.nfc_setting_layout));
	            new AlertDialog.Builder(this).setView(dialoglayout)
	            	    .setPositiveButton("Update Settings", new DialogInterface.OnClickListener() {
			                public void onClick(DialogInterface arg0, int arg1) {
			    				Intent setnfc = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
			    				startActivity(setnfc);
			                }
		                })
	                    .setOnCancelListener(new DialogInterface.OnCancelListener() {
	                        
	                    	public void onCancel(DialogInterface dialog) {
	                    		finish(); // exit application if user cancels
	                        }	                    	
	                    }).create().show();

			} else {
				mNfcAdapter.enableForegroundDispatch(this, mPendingIntent,
						mIntentFilters, null);
			}
			
		} else {
			Toast.makeText(getApplicationContext(), "Sorry, No NFC Adapter found.", Toast.LENGTH_SHORT).show();
		}
		

	}

}
